﻿<%@ WebHandler Language="C#" Class="ApsPreProducePreparationHandler" %>

using System;
using System.Web;
using BusinessRule.DTO;
using System.Web.SessionState;
using DatabaseAccess.DBUtility;
using System.Data.OracleClient;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using Newtonsoft.Json;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;

//Description:预排产转工任务处理程序
//Copyright (c) : 通力凯顿（北京）系统集成有限公司
//Writer:longb
//create Date:2023-07-08

public class ApsPreProducePreparationHandler : IHttpHandler, IRequiresSessionState
{
    CurrentUserInfo m_UserInfo;
    uLMSCommonBusiness commonBusiness = new uLMSCommonBusiness();
    private LimitedCapacityBusiness m_LMObj = new LimitedCapacityBusiness();
    private RbApsLimitedCapacityBusiness m_RbAps = new RbApsLimitedCapacityBusiness();
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            switch (action)
            {
                case "getApsProducePrepData": //查询生产准备任务信息
                    getApsProducePrepData(context, "");
                    break;
                case "getApsProducePrepDetailData": //查询生产准备--明细信息
                    getApsProducePrepDetailData(context);
                    break;
                case "reviewDetailData"://复查
                    reviewDetailData(context);
                    break;
                case "exprotData"://预排齐套导出
                    exprotData(context);
                    break;
                case "exprotDetailData"://清单明细导出
                    exprotDetailData(context);
                    break;
                case "initPageData"://初始化数据
                    initPageData(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            var result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
   


    /// <summary>
    /// 获取排产池列表
    /// </summary>
    /// <param name="context"></param>
    public void initPageData(HttpContext context)
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para["FactoryID"] = m_UserInfo.FactoryID;
        para["EmployeeID"] = m_UserInfo.EmployeeID.ToString();
        DataTable dt = m_LMObj.GetPoolList(para);
        var result = new uMESProcessResult();
        result.DBTable = dt;
        result.ReturnData = m_UserInfo.FactoryID;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 查询生产准备任务信息
    /// </summary>
    /// <param name="context"></param>
    public void getApsProducePrepData(HttpContext context, string ContainerIDS)
    {
        var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(context.Request["data"].ToString());
        dic["factoryId"] = m_UserInfo.FactoryID;
        dic["pageIndex"] = context.Request["pageNumber"].ToString();
        dic["pageSize"] = context.Request["pageSize"].ToString();
        uMESProcessResult result  = m_RbAps.GetApsProducePrepData(dic, true);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    public void exprotData(HttpContext context)
    {
        var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(context.Request["data"].ToString());
        dic["factoryId"] = m_UserInfo.FactoryID;
        DataTable dt = m_RbAps.GetApsProducePrepData(dic, false).DBTable;
        DataTable dtExport = dt.AsDataView().ToTable(false, "MFGORDERNAME", "CONTAINERNAME", "FACTORYNAME", "ASSISFACTORYNAME", "WORKFLOWSTEPNAME", "FULLNAME", "STARTTIME", "ENDTIME", "SEQUENCE", "DURATION", "ISWORKFLOW", "ISAPS", "ISTOOL", "ISMATERIAL", "ISRESOURCE", "ISSPOTCHECK");//只保留需导出的列
        dtExport.Columns["MFGORDERNAME"].ColumnName = "订单号";
        dtExport.Columns["CONTAINERNAME"].ColumnName = "批次";
        dtExport.Columns["FACTORYNAME"].ColumnName = "主制分厂";
        dtExport.Columns["ASSISFACTORYNAME"].ColumnName = "承制分厂";
        dtExport.Columns["WORKFLOWSTEPNAME"].ColumnName = "转工工序";
        dtExport.Columns["FULLNAME"].ColumnName = "转出人";
        dtExport.Columns["STARTTIME"].ColumnName = "转工计划开始";
        dtExport.Columns["ENDTIME"].ColumnName = "转工计划结束";
        dtExport.Columns["SEQUENCE"].ColumnName = "顺序号";
        dtExport.Columns["DURATION"].ColumnName = "期量(h)";
        dtExport.Columns["ISWORKFLOW"].ColumnName = "工艺路线";
        dtExport.Columns["ISAPS"].ColumnName = "排产数据";
        dtExport.Columns["ISTOOL"].ColumnName = "工装工具";
        dtExport.Columns["ISMATERIAL"].ColumnName = "生产物资";
        dtExport.Columns["ISRESOURCE"].ColumnName = "设备状态";
        dtExport.Columns["ISSPOTCHECK"].ColumnName = "特殊过程";
        excuteExport(context, dtExport, "转工预排产齐套性检查");
    }
    /// <summary>
    /// 清单导出
    /// </summary>
    /// <param name="context"></param>
    public void exprotDetailData(HttpContext context)
    {
        var dic = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString()).Rows[0];
        var index = dic["index"].ToString();
        DataTable dtExport = null;
        string titleExport = string.Empty;
        var IsComp = 1;
        DataTable sonDT = m_RbAps.GetSonStepWorkflow(m_UserInfo.FactoryID,dic["WORKFLOWSTEPID"].ToString());
        switch (index)
        {
            case "0"://工艺生产准备清单
                dtExport = m_RbAps.GetWorkflowProData(dic, sonDT, ref IsComp);
                dtExport.Columns["WORKFLOWREVISION"].ColumnName = "版次";
                dtExport.Columns["WORKFLOWTYPE"].ColumnName = "工艺类型";
                titleExport = "工艺生产准备清单";
                break;
            case "1"://排产数据异常清单
                dtExport = m_RbAps.GetApsExceptionData(dic, sonDT, ref IsComp);
                dtExport.Columns["ISCOMP"].ColumnName = "缺设备信息";
                dtExport.Columns["ISCOMP2"].ColumnName = "缺设备时间";
               
                dtExport.Columns.Remove("PRODUCTID");
                dtExport.Columns.Remove("WORKFLOWID");
                titleExport = "工艺生产准备清单";
                break;
            case "2"://工装工具缺件清单
                dtExport = m_RbAps.GetToolData(dic, sonDT, ref IsComp);
                dtExport.Columns["TOOLNAME"].ColumnName = "工装编号";
                dtExport.Columns["DESCRIPTION"].ColumnName = "工装名称";
                dtExport.Columns["RESOURCETYPENAME"].ColumnName = "工装类型";
                dtExport.Columns["QTY"].ColumnName = "需求数量";
                dtExport.Columns["LABSTALL"].ColumnName = "库存数量";
                dtExport.Columns.Remove("PRODUCTID");
                dtExport.Columns.Remove("WORKFLOWID");
                titleExport = "工装工具缺件清单";
                break;
            case "3"://工序主辅材缺料清单
                dtExport = m_RbAps.GetAuxMaterialData(dic, sonDT, "1", ref IsComp);
                dtExport.Columns["AUXMATERIALNAME"].ColumnName = "物料编号";
                dtExport.Columns["DESCRIPTION"].ColumnName = "物料名称";
                dtExport.Columns["QTY"].ColumnName = "需求数量";
                dtExport.Columns["LABSTALL"].ColumnName = "库存数量";
                dtExport.Columns.Remove("PRODUCTID");
                dtExport.Columns.Remove("WORKFLOWID");
                titleExport = "工序主辅材缺料清单";
                break;
            case "4"://主辅材汇总缺料清单
                dtExport = m_RbAps.GetAuxMaterialData(dic, sonDT, "", ref IsComp);
                dtExport.Columns["AUXMATERIALNAME"].ColumnName = "物料编号";
                dtExport.Columns["DESCRIPTION"].ColumnName = "物料名称";
                dtExport.Columns["QTY"].ColumnName = "需求数量";
                dtExport.Columns["LABSTALL"].ColumnName = "库存数量";
                dtExport.Columns["DIFFQTY"].ColumnName = "差额";
                dtExport.Columns.Remove("WORKFLOWSTEPID");
                titleExport = "主辅材汇总缺料清单";
                break;
            case "5"://设备状态异常清单
                dtExport = m_RbAps.GetResourceData(dic, sonDT, ref IsComp);
                dtExport.Columns["RESOURCENAME"].ColumnName = "设备编号";
                dtExport.Columns["DESCRIPTION"].ColumnName = "设备名称";
                dtExport.Columns["RESOURCETYPENAME"].ColumnName = "设备类型";
                dtExport.Columns["ERPSTATUS"].ColumnName = "设备状态";
                titleExport = "设备状态异常清单";
                break;
            case "6"://特殊过程异常清单
                dtExport = m_RbAps.GetSpotcheckData(dic, sonDT, ref IsComp);
                dtExport.Columns["SPECIALCOURSESPECREPORTNO"].ColumnName = "特殊过程确认编号";
                dtExport.Columns["INDATE"].ColumnName = "有效期";
                titleExport = "特殊过程异常清单";
                break;
            case "7"://质量预警
                dtExport = m_RbAps.GetQualityData(dic, sonDT, ref IsComp);
                dtExport.Columns["MFGORDERNAME"].ColumnName = "订单";
                dtExport.Columns["CONTAINERNAME"].ColumnName = "热表批次";
                dtExport.Columns["WORKFLOWSTEPNAME"].ColumnName = "工序";
                dtExport.Columns["REPAIRQTY"].ColumnName = "返修数";
                dtExport.Columns["LOSSQTY"].ColumnName = "报废数";
                dtExport.Columns["RECTIFYMEASURES"].ColumnName = "整改措施";
                titleExport = "质量预警";
                break;
            case "8"://数控程序
                dtExport = m_RbAps.GetNcProgramData(dic, sonDT, ref IsComp);
                dtExport.Columns["NC_NO"].ColumnName = "数控程序编号";
                dtExport.Columns["NC_NAME"].ColumnName = "数控程序名称";
                dtExport.Columns["MATERIAL_MARK"].ColumnName = "加工材料";
                dtExport.Columns["CONTROL_TYPE"].ColumnName = "控制系统";
                dtExport.Columns["ISTHREE"].ColumnName = "是否三轴以上数控铣加工";
                dtExport.Columns["ISTYPICAL"].ColumnName = "是否关键工序";
                dtExport.Columns["EQPIPORFILENAME"].ColumnName = "设备编号/工艺附件";
                titleExport = "数控程序";
                break;
        }
        if (dtExport != null && dtExport.Columns.Contains("ISCOMP")) { dtExport.Columns.Remove("ISCOMP"); }
        if (dtExport != null && dtExport.Columns.Contains("SONPRODUCTNAME")) { dtExport.Columns["SONPRODUCTNAME"].ColumnName = "热表件号"; }
        if (!index.Equals("4") && dtExport != null)
        {
            dtExport.Columns["FACTORYNAME"].ColumnName = "主制分厂";
            dtExport.Columns["STARTTIME"].ColumnName = "转工计划开始";
            dtExport.Columns["PRODUCTNAME"].ColumnName = "件号";
            dtExport.Columns["WORKFLOWSTEPNAME"].ColumnName = "转工工序";
            dtExport.Columns["WORKFLOWINFO"].ColumnName = "热表工艺路线";
        }
        if (dtExport == null || dtExport.Rows.Count == 0) return;
        excuteExport(context, dtExport, titleExport);
    }


    public void excuteExport(HttpContext context, DataTable dtExport, string title)
    {
        byte[] bytes = uLMS_CommonFunction.DataTableExportToExcel(dtExport, title);
        context.Response.Clear();
        context.Response.Buffer = true;
        context.Response.ClearHeaders();
        context.Response.ClearContent();
        context.Response.ContentType = "application/vnd.ms-excel";
        context.Response.AddHeader("fileDownload", "1");
        context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}{1}.xls", context.Server.UrlEncode(title), DateTime.Now.ToString("yyyyMMddHHmmssffff")));
        context.Response.AddHeader("Content-Length", bytes.Length.ToString());
        context.Response.AddHeader("Content-Type", "UTF-8");
        context.Response.Charset = "UTF-8";
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;
        context.Response.BinaryWrite(bytes);
        context.Response.Flush();
        context.Response.Close();
    }

    /// <summary>
    /// 查询生产准备--明细信息
    /// </summary>
    public void getApsProducePrepDetailData(HttpContext context)
    {
        var dic = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString()).Rows[0];
        var ContainerID = dic["CONTAINERID"].ToString();
        var workflowid = dic["WORKFLOWID"].ToString();
        var workflowstepid = dic["WORKFLOWSTEPID"].ToString();
        var result = new uMESProcessResult();
        DataSet set = new DataSet();
        int IsComp = 0;
        DataTable sonDT = m_RbAps.GetSonStepWorkflow(m_UserInfo.FactoryID,workflowstepid);
        if (sonDT.Rows.Count > 0) { }
        //工艺生产准备数据 
        DataTable WorkflowPro =m_RbAps.GetWorkflowProData(dic, sonDT, ref IsComp);
        WorkflowPro.TableName = "WorkflowProTable";
        set.Tables.Add(WorkflowPro);
        //排产异常数据 
        DataTable aps = m_RbAps.GetApsExceptionData(dic, sonDT, ref IsComp);
        aps.TableName = "ApsTable";
        set.Tables.Add(aps);
        //工装工具数据 
        DataTable Tool = m_RbAps.GetToolData(dic, sonDT, ref IsComp);
        Tool.TableName = "ToolTable";
        set.Tables.Add(Tool);
        //工序辅材数据 
        DataTable WsAuxMaterial = m_RbAps.GetAuxMaterialData(dic, sonDT, "1", ref IsComp);
        WsAuxMaterial.TableName = "WsAuxMaterialTable";
        set.Tables.Add(WsAuxMaterial);
        //辅材数据 
        DataTable AuxMaterial = m_RbAps.GetAuxMaterialData(dic, sonDT, "", ref IsComp);
        AuxMaterial.TableName = "AuxMaterialTable";
        set.Tables.Add(AuxMaterial);
        //设备数据 
        DataTable Control = m_RbAps.GetResourceData(dic, sonDT, ref IsComp);
        Control.TableName = "ResourceTable";
        set.Tables.Add(Control);
        //特殊过程数据
        DataTable Spotcheck = m_RbAps.GetSpotcheckData(dic, sonDT, ref IsComp);
        Spotcheck.TableName = "SpotcheckTable";
        set.Tables.Add(Spotcheck);
        //质量预警
        DataTable Quality = m_RbAps.GetQualityData(dic, sonDT, ref IsComp);
        Quality.TableName = "QualityTable";
        set.Tables.Add(Quality);

        DataTable NcProgram = m_RbAps.GetNcProgramData(dic, sonDT, ref IsComp);
        NcProgram.TableName = "NcProgramTable";
        set.Tables.Add(NcProgram);
        result.DBDataSet = set;
        result.Result = true;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
   
    /// <summary>
    /// 复查
    /// </summary>
    /// <param name="context"></param>
    public void reviewDetailData(HttpContext context)
    {
        var dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var result = new uMESProcessResult();
        result.Result = m_RbAps.ReviewDetailData(dt,m_UserInfo.FactoryID,m_UserInfo.EmployeeID);//DatabaseHelper.ExecuteSqlListTran(arrList);
        result.Message = result.Result ? "复查成功" : "复查失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

}